home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_11_12 / kauffman / debug.cpp < prev    next >
C/C++ Source or Header  |  1993-05-09  |  2KB  |  114 lines

  1. Listing 2 (debug.cpp)
  2. =====================
  3.  
  4. MyDebugStream::MyDebugStream(
  5.  
  6. // default output specifier
  7.     BOOL UseMessageBox,
  8.  
  9. // possible title for MessageBox
  10.     char* pTitle )
  11.  
  12. // pass arguments to MyDebugStreambuf constructor
  13.     : m_buf( UseMessageBox, pTitle ),
  14.  
  15. // pass MyDebugStreambuf to ostream constructor
  16.       ostream( (streambuf*) &m_buf )
  17. {
  18.     ASSERT( this );
  19. }
  20.  
  21. MyDebugStreambuf::MyDebugStreambuf(
  22.     BOOL UseMessageBox,
  23.     char* pTitle )
  24.  
  25.     : strstreambuf()
  26. {
  27.     ASSERT( this );
  28. // store the default output flag
  29.     m_UseMessageBox = UseMessageBox;
  30.     
  31. // store pointer to the MessageBox title
  32.     m_pTitle = pTitle;
  33.     
  34. // store the buffer size
  35.     m_BufferSize = DEBUG_BUFFER_SIZE;
  36.     
  37. // get one extra for NULL terminator
  38.     m_pBuffer = new char[m_BufferSize + 1];
  39.     
  40. // paranoia check
  41.     ASSERT( m_pBuffer );
  42.     
  43. // init put pointers
  44.     setp( m_pBuffer, m_pBuffer + m_BufferSize );
  45. }
  46.  
  47. int MyDebugStreambuf::overflow( int ch )
  48. {
  49.     ASSERT( this );
  50.     if( out_waiting() )
  51. // data in buffer
  52.         Flush();
  53.     return strstreambuf::overflow( ch );
  54. }
  55.  
  56. int MyDebugStreambuf::sync()
  57. {
  58.     ASSERT( this );
  59.     if( out_waiting() )
  60. // data in buffer
  61.         Flush();
  62.     return strstreambuf::sync();
  63. }
  64.  
  65. void MyDebugStreambuf::Flush()
  66. {
  67.     ASSERT( this );
  68. // get default
  69.     BOOL UseMessageBox = m_UseMessageBox;
  70.  
  71. // get pointer to last character
  72.     char* p = pptr() - 1;
  73.     if( *p == (char) FLUSH_TO_BOX )
  74.     {
  75. // set output flag
  76.         UseMessageBox = TRUE;
  77.  
  78. // discard FLUSH_TO_BOX
  79.         *p = '\0';
  80.     }
  81.     else if( *p == (char) FLUSH_TO_DEBUG )
  82.     {
  83. // set output flag
  84.         UseMessageBox = FALSE;
  85.         
  86. // discard FLUSH_TO_DEBUG
  87.         *p = '\0';
  88.     }
  89.     else
  90. // NULL terminate
  91.         *pptr() = '\0';
  92.  
  93. // do the output
  94.     if( UseMessageBox )
  95.         MessageBox( NULL, m_pBuffer, m_pTitle,
  96.                 MB_OK|MB_ICONINFORMATION );
  97.     else
  98.         ::OutputDebugString( m_pBuffer );
  99.  
  100. // reset buffer pointers
  101.     setp( m_pBuffer, m_pBuffer + m_BufferSize );
  102. }
  103.  
  104. ostream& BoxFlush( ostream& os )
  105. {
  106.     return os << (char) FLUSH_TO_BOX << flush;
  107. }
  108.  
  109. ostream& DebugFlush( ostream& os )
  110. {
  111.     return os << "\r\n"
  112.           << (char) FLUSH_TO_DEBUG << flush;
  113. }
  114.